home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_gen / freeli10.zip / FREELIB3.ASX < prev    next >
Text File  |  1996-04-20  |  65KB  |  2,282 lines

  1. ~~~C_TXMAIN
  2. Ideal
  3.  
  4. Include     "textmac.inc"           ;Include macros
  5.  
  6. Public      CurSeg, W_X1, W_Y1, W_X2, W_Y2, W_Rel, Attr
  7. Public      inittext, closetext, clrscr, setcolor, getcolor
  8.  
  9. Model Tiny
  10. P186
  11. CodeSeg
  12.  
  13. ;****************** Data Section
  14.  
  15. EGAPal      dw 8010h,0000h,0101h    ;EGA palette
  16.             dw 0202h,0303h,0404h
  17.             dw 0505h,0606h,0707h
  18.             dw 0808h,0909h,0A0Ah
  19.             dw 0B0Bh,0C0Ch,0D0Dh
  20.             dw 0E0Eh,0F2Fh
  21.  
  22. DACPal      db 0,0,0,0,37,0,0,63    ;DAC palette:  New color set
  23.             db 0,37,63,37,0,0,63    ;with much more variety than
  24.             db 37,0,37,63,0,63,46   ;the standard EGA color set.
  25.             db 46,46,27,27,27,63    ;Also allows XOR cursor 07h
  26.             db 0,0,63,37,0,63,63    ;with 67% minimum contrast
  27.             db 0,0,37,63,0,0,37     ;and 75% average contrast.
  28.             db 0,63,63,63,63,63
  29.  
  30. CurSeg      dw 0                    ;Current segment
  31.  
  32. W_X1        dw 0                    ;Window values
  33. W_Y1        dw 0
  34. W_X2        dw 0
  35. W_Y2        dw 0
  36. W_Rel       dw 0
  37.  
  38. Attr        dw 0                    ;Current attribute
  39.  
  40. ;****************** inittext() -- Init text system
  41. ;void inittext(void);
  42.  
  43. Proc        inittext
  44.  
  45.             pusha                   ;Save all registers
  46.             push es
  47.  
  48.             push 0A000h             ;ES = video memory
  49.             pop es
  50.  
  51.             mov ax,3                ;Set 80x25 text mode
  52.             int 10h
  53.  
  54.             mov ax,1114h            ;Load standard 8x16 font
  55.             xor bl,bl
  56.             int 10h
  57.  
  58.             call SetSize            ;Set resolution
  59.  
  60.             mov dx,03D4h            ;CRTC port
  61.             mov ax,4013h            ;Fake 128 chars
  62.             out dx,ax               ;for efficiency
  63.  
  64.             mov dx,03DAh            ;IS1 port
  65.             in al,dx                ;Reset AC
  66.             mov dx,03C0h            ;AC port
  67.             mov si,offset EGAPal    ;Send palette
  68.             mov cx,34               ;via REP OUTSB
  69.             rep outsb
  70.  
  71.             mov dx,03C8h            ;DACWA port
  72.             xor al,al               ;Set address 0
  73.             out dx,al
  74.             inc dx                  ;DACD port
  75.             mov si,offset DACPal    ;Send palette
  76.             mov cx,48               ;via REP OUTSB
  77.             rep outsb
  78.  
  79.             mov [W_X2],89           ;Set variables:
  80.             mov [W_Y2],33           ;W_X2, W_Y2...
  81.  
  82.             xor ax,ax
  83.             mov [W_X1],ax           ;W_X1...
  84.             mov [W_Y1],ax           ;W_Y1...
  85.             mov [W_Rel],ax          ;W_Rel...
  86.             mov [Attr],07h          ;Attr...
  87.             mov [CurSeg],es         ;CurSeg...
  88.  
  89.             xor di,di               ;Clear video memory
  90.             mov cx,8000h
  91.             mov ax,0720h
  92.             rep stosw
  93.  
  94. in_done:    pop es                  ;Restore registers
  95.             popa
  96.             ret                     ;Return
  97.  
  98. EndP        inittext
  99.  
  100. ;****************** closetext() -- Close text system
  101. ;void closetext(void);
  102.  
  103. Proc        closetext
  104.  
  105.             push ax                 ;Save AX
  106.             mov ax,3                ;Set text video mode
  107.             int 10h
  108.             pop ax                  ;Restore AX
  109.             ret                     ;Return
  110.  
  111. EndP        closetext
  112.  
  113. ;****************** clrscr() -- Clear entire screen
  114. ;void clrscr(void);
  115.  
  116. Proc        clrscr
  117.  
  118.             pusha                   ;Save registers
  119.             push es
  120.  
  121.             mov es,[CurSeg]         ;ES = video memory
  122.  
  123.             xor di,di               ;Clear video page
  124.             mov cx,2800h
  125.             mov ax,0720h
  126.             rep stosw
  127.  
  128.             pop es                  ;Restore registers
  129.             popa
  130.             ret                     ;Return
  131.  
  132. EndP        clrscr
  133.  
  134. ;****************** SetSize -- Set 90x34 resolution
  135.  
  136. Proc        SetSize
  137.  
  138.             pusha                   ;Save all registers
  139.             mov dx,03C4h            ;Sequencer Port
  140.             mov ax,0100h            ;Synchronous reset
  141.             out dx,ax               ;Send command
  142.             inc ax                  ;8 pixels/char
  143.             out dx,ax               ;Send command
  144.  
  145.             mov dx,03CCh            ;Misc Output Read Port
  146.             in al,dx                ;Read value
  147.             and al,0F3h             ;Bits 2-3: 01 = 28MHz
  148.             or al,0C4h              ;Bits 6-7: 11 = 480 scanlines
  149.             mov dx,03C2h            ;Misc Output Write Port
  150.             out dx,al               ;Send command
  151.  
  152.             mov dx,03DAh            ;Read this port to reset
  153.             in al,dx                ;the Attribute Controller
  154.             mov dx,03C0h            ;Attribute Controller Port
  155.             mov al,33h              ;Horizontal Panning
  156.             out dx,al               ;Select register
  157.             xor al,al               ;Set Shift = 0
  158.             out dx,al               ;Send command
  159.  
  160.             mov dx,03D4h            ;CRT Controller Port
  161.             mov si,offset Data90x34 ;Offset of CRTC data
  162.             mov cx,17               ;17 words
  163.             rep outsw               ;Send data
  164.  
  165.             mov dx,03C4h            ;Sequencer Port
  166.             mov ax,0300h            ;Restart Sequencer
  167.             out dx,ax               ;Send command
  168.  
  169.             mov si,offset DataOE    ;Turning off Odd/Even...
  170.             mov dx,03C4h            ;SC port
  171.             mov cx,2                ;Send 2 words to SC port
  172.             rep outsw
  173.             mov dx,03CEh            ;GC port
  174.             mov cx,3                ;Send 3 words to GC port
  175.             rep outsw
  176.             push si                 ;Save SI
  177.  
  178.             mov si,1                ;Setting a 14-line Font.
  179.             xor di,di               ;Shifting up by 1
  180.             mov dx,256              ;All 256 chars
  181.             xor al,al               ;Zero AL
  182.  
  183. SS_sfont:   mov cx,14               ;14 bytes
  184.             seges rep movsb         ;Shift memory
  185.             mov cx,18               ;18 bytes
  186.             add si,cx               ;Next char slot
  187.             rep stosb               ;Clear excess
  188.             dec dx                  ;Loop back
  189.             jnz SS_sfont
  190.  
  191.             pop si                  ;Restoring Odd/Even...
  192.             mov dx,03C4h            ;SC port
  193.             mov cx,2                ;Send 2 words to SC port
  194.             rep outsw
  195.             mov dx,03CEh            ;GC port
  196.             mov cx,3                ;Send 3 words to GC port
  197.             rep outsw
  198.  
  199.             popa                    ;Restore registers
  200.             ret                     ;Return
  201.  
  202. Data90x34   dw 00011h               ;De-protect value
  203.             dw 06B00h,05901h,05A02h ;Horizontal CRTC data
  204.             dw 08E03h,06004h,08D05h
  205.             dw 00B06h,03E07h,04D09h ;Vertical CRTC data
  206.             dw 0EA10h,08C11h,0DF12h
  207.             dw 0E715h,00416h
  208.             dw 00C0Ah,00D0Bh        ;Initial cursor type
  209.  
  210. DataOE      dw 0402h,0704h,0204h    ;Odd/Even release and
  211.             dw 0005h,0406h          ;0A000h segment data
  212.             dw 0302h,0304h,0004h    ;Odd/Even restore data
  213.             dw 1005h,0606h
  214.  
  215. EndP        SetSize
  216.  
  217. ;****************** setcolor() -- Set text attribute
  218. ;void setcolor(int at);
  219.  
  220. Proc        setcolor
  221.  
  222.             push bp                 ;Set up stack frame
  223.             mov bp,sp
  224.  
  225.             push [bp+4]             ;Set attribute
  226.             pop [Attr]
  227.  
  228.             pop bp                  ;Delete stack frame
  229.             ret 2                   ;Return
  230.  
  231. EndP        setcolor
  232.  
  233. ;****************** getcolor() -- Get text attribute
  234. ;int getcolor(void);
  235.  
  236. Proc        getcolor
  237.  
  238.             mov ax,[Attr]           ;Get attribute
  239.             ret                     ;Return
  240.  
  241. EndP        getcolor
  242.  
  243. End
  244.  
  245. ~~~C_TXWIN
  246. Ideal
  247.  
  248. Include     "textmac.inc"           ;Include macros
  249. Include     "textdat.inc"           ;Include data
  250.  
  251. Public      setwin, clrwin, fbox
  252.  
  253. Model Tiny
  254. P186
  255. CodeSeg
  256.  
  257. ;****************** setwin() -- Set text window position, type
  258. ;void setwin(int x1, int y1, int x2, int y2, int rel);
  259.  
  260. Proc        setwin
  261.  
  262.             push bp                 ;Set up stack frame
  263.             mov bp,sp
  264.  
  265.             push [bp+12]            ;Set W_X1
  266.             pop [W_X1]
  267.             push [bp+10]            ;Set W_Y1
  268.             pop [W_Y1]
  269.             push [bp+8]             ;Set W_X2
  270.             pop [W_X2]
  271.             push [bp+6]             ;Set W_Y2
  272.             pop [W_Y2]
  273.             push [bp+4]             ;Set W_Rel
  274.             pop [W_Rel]
  275.  
  276.             pop bp                  ;Delete stack frame
  277.             ret 10                  ;Return
  278.  
  279. EndP        setwin
  280.  
  281. ;****************** clrwin() -- Clear window
  282. ;void clrwin(void);
  283.  
  284. Proc        clrwin
  285.  
  286.             pusha                   ;Save all registers
  287.  
  288.             mov ax,[W_X1]           ;Get values
  289.             mov bx,[W_Y1]
  290.             mov cx,[W_X2]
  291.             mov dx,[W_Y2]
  292.  
  293.             cmp [W_Rel],0           ;Relativity check
  294.             je clw_norel
  295.  
  296.             xor ax,ax               ;Adjust to window
  297.             xor bx,bx
  298.             sub cx,ax
  299.             sub dx,bx
  300.  
  301. clw_norel:  push ax bx cx dx ' '    ;fbox(W_X1, W_Y1,
  302.             call fbox               ;     W_X2, W_Y2, ' ');
  303.  
  304.             popa                    ;Restore registers
  305.             ret                     ;Return
  306.  
  307. EndP        clrwin
  308.  
  309. ;****************** fbox() -- Draw filled box
  310. ;void fbox(int x1, int y1, int x2, int y2, int ch);
  311.  
  312. Proc        fbox
  313.  
  314.             push bp                 ;Set up stack frame
  315.             mov bp,sp
  316.             pusha                   ;Save registers
  317.             push es
  318.  
  319.             mov es,[CurSeg]         ;ES = video memory
  320.  
  321.             mov ax,[bp+12]          ;Get values
  322.             mov bx,[bp+10]
  323.             mov cx,[bp+8]
  324.             mov dx,[bp+6]
  325.  
  326.             cmp [W_Rel],0           ;Relativity check
  327.             je fbx_norel
  328.  
  329.             add ax,[W_X1]           ;Adjust to window
  330.             add bx,[W_Y1]
  331.             add cx,[W_X1]
  332.             add dx,[W_Y1]
  333.  
  334. fbx_norel:  cmp ax,cx               ;X1 > X2?
  335.             jle fbx_xok
  336.             xchg ax,cx              ;Switch them
  337.  
  338. fbx_xok:    cmp bx,dx               ;Y1 > Y2?
  339.             jle fbx_yok
  340.             xchg bx,dx              ;Switch them
  341.  
  342. fbx_yok:    cmp ax,[W_X2]           ;X1 > W_X2?
  343.             jg fbx_bad
  344.             cmp cx,[W_X1]           ;X2 < W_X1?
  345.             jl fbx_bad
  346.             cmp bx,[W_Y2]           ;Y1 > W_Y2?
  347.             jg fbx_bad
  348.             cmp dx,[W_Y1]           ;Y2 < W_Y1?
  349.             jnl fbx_cx1
  350.  
  351. fbx_bad:    jmp fbx_done
  352.  
  353. fbx_cx1:    cmp ax,[W_X1]           ;X1 < W_X1?
  354.             jnl fbx_cx2
  355.             mov ax,[W_X1]           ;Fix X1
  356.  
  357. fbx_cx2:    cmp cx,[W_X2]           ;X2 > W_X2?
  358.             jng fbx_cy1
  359.             mov cx,[W_X2]           ;Fix X2
  360.  
  361. fbx_cy1:    cmp bx,[W_Y1]           ;Y1 < W_Y1?
  362.             jnl fbx_cy2
  363.             mov bx,[W_Y1]           ;Fix Y1
  364.  
  365. fbx_cy2:    cmp dx,[W_Y2]           ;Y2 > W_Y2?
  366.             jng fbx_cont
  367.             mov dx,[W_Y2]           ;Fix Y2
  368.  
  369. fbx_cont:   mov si,dx               ;SI = Y distance
  370.             sub si,bx
  371.             inc si
  372.             mov dx,cx               ;DX = X distance
  373.             sub dx,ax
  374.             inc dx
  375.  
  376.             mov cx,bx               ;BX, CX = X1, Y1
  377.             mov bx,ax
  378.             mov al,[bp+4]           ;AL = char
  379.             mov ah,[byte Attr]      ;AH = attr
  380.  
  381. fbx_loop:   SET_ROW_CHA             ;Draw row
  382.             inc cx                  ;Next line
  383.             dec si                  ;Loop back
  384.             jnz fbx_loop
  385.  
  386. fbx_done:   pop es                  ;Restore registers
  387.             popa
  388.             pop bp                  ;Delete stack frame
  389.             ret 10                  ;Return
  390.  
  391. EndP        fbox
  392.  
  393. End
  394.  
  395. ~~~C_TXPAGE
  396. Ideal
  397.  
  398. Include     "textmac.inc"           ;Include macros
  399. Include     "textdat.inc"           ;Include data
  400.  
  401. Public      setpage, getpage
  402.  
  403. Model Tiny
  404. P186
  405. CodeSeg
  406.  
  407. ;****************** setpage() -- Set video page
  408. ;void setpage(int page);
  409.  
  410. Proc        setpage
  411.  
  412.             push bp                 ;Set up stack frame
  413.             mov bp,sp
  414.             pusha                   ;Save all registers
  415.  
  416.             mov ax,[bp+4]           ;AX = page number
  417.             db 0D4h,06h             ;AAM 6 (TASM doesn't recognize it)
  418.             xor ah,ah               ;this puts it between 0 and 5
  419.             push ax
  420.  
  421.             imul ax,320             ;AX = segment
  422.             add ax,0A000h
  423.             mov [CurSeg],ax         ;Store segment
  424.  
  425.             pop bx                  ;BX = page number
  426.             imul bx,20              ;BX = high byte value
  427.  
  428.             mov dx,03C4h            ;CRTC port
  429.             mov al,0Ch              ;Start Address High
  430.             mov ah,bl               ;AH = high byte value
  431.             out dx,ax               ;Send command
  432.  
  433.             popa                    ;Restore registers
  434.             pop bp                  ;Delete stack frame
  435.             ret 2                   ;Return
  436.  
  437. EndP        setpage
  438.  
  439. ;****************** getpage() -- Get video page
  440. ;int getpage(void);
  441.  
  442. Proc        getpage
  443.  
  444.             mov ax,[CurSeg]         ;Get segment pointer
  445.             sub ax,0A000h           ;Subtract video segment
  446.             shr ax,6                ;Divide by 64
  447.             db 0D4h,05h             ;AAM 5 (TASM doesn't recognize it)
  448.             shr ax,8                ;now AX = page number
  449.             ret                     ;Return
  450.  
  451. EndP        getpage
  452.  
  453. End
  454.  
  455. ~~~C_THLINE
  456. Ideal
  457.  
  458. Include     "textmac.inc"           ;Include macros
  459. Include     "textdat.inc"           ;Include data
  460.  
  461. Public      hline
  462.  
  463. Model Tiny
  464. P186
  465. CodeSeg
  466.  
  467. ;****************** hline() -- Draw horizontal line
  468. ;void hline(int x1, int x2, int y, int ch);
  469.  
  470. Proc        hline
  471.  
  472.             push bp                 ;Set up stack frame
  473.             mov bp,sp
  474.             push es                 ;Save registers
  475.             pusha
  476.             mov es,[CurSeg]         ;ES = video memory
  477.  
  478.             mov ax,[bp+10]          ;Get values
  479.             mov bx,[bp+6]
  480.             mov cx,[bp+8]
  481.  
  482.             cmp [W_Rel],0           ;Relativity check
  483.             je hl_norel
  484.  
  485.             add ax,[W_X1]           ;Adjust to window
  486.             add bx,[W_Y1]
  487.             add cx,[W_X1]
  488.  
  489. hl_norel:   cmp ax,cx               ;X1 > X2?
  490.             jle hl_xok
  491.             xchg ax,cx              ;Switch them
  492.  
  493. hl_xok:     cmp bx,[W_Y2]           ;Y > W_Y2?
  494.             jg hl_done
  495.             cmp bx,[W_Y1]           ;Y < W_Y1?
  496.             jl hl_done
  497.             cmp ax,[W_X2]           ;X1 > W_X2?
  498.             jg hl_done
  499.             cmp cx,[W_X1]           ;X2 < W_X1?
  500.             jl hl_done
  501.  
  502. hl_cx1:     cmp ax,[W_X1]           ;X1 < W_X1?
  503.             jnl hl_cx2
  504.             mov ax,[W_X1]           ;Fix X1
  505.  
  506. hl_cx2:     cmp cx,[W_X2]           ;X2 > W_X2?
  507.             jng hl_cont
  508.             mov cx,[W_X2]           ;Fix X2
  509.  
  510. hl_cont:    mov [bp+10],ax          ;Store end-points
  511.             mov [bp+6],bx
  512.             mov [bp+8],cx
  513.  
  514.             mov bx,[bp+10]          ;BX = X
  515.             mov cx,[bp+6]           ;CX = Y
  516.             mov dx,[bp+8]           ;DX = count
  517.             sub dx,bx
  518.             inc dx
  519.             mov al,[bp+4]           ;AL = char
  520.             mov ah,[byte Attr]      ;AH = attr
  521.  
  522.             SET_ROW_CHA             ;Draw row
  523.  
  524. hl_done:    popa                    ;Restore registers
  525.             pop es
  526.             pop bp                  ;Delete stack frame
  527.             ret 8                   ;Return
  528.  
  529. EndP        hline
  530.  
  531. End
  532.  
  533. ~~~C_TVLINE
  534. Ideal
  535.  
  536. Include     "textmac.inc"           ;Include macros
  537. Include     "textdat.inc"           ;Include data
  538.  
  539. Public      vline
  540.  
  541. Model Tiny
  542. P186
  543. CodeSeg
  544.  
  545. ;****************** vline() -- Draw vertical line
  546. ;void vline(int y1, int y2, int x, int ch);
  547.  
  548. Proc        vline
  549.  
  550.             push bp                 ;Set up stack frame
  551.             mov bp,sp
  552.             push es                 ;Save registers
  553.             pusha
  554.             mov es,[CurSeg]         ;ES = video memory
  555.  
  556.             mov ax,[bp+10]          ;Get values
  557.             mov bx,[bp+6]
  558.             mov cx,[bp+8]
  559.  
  560.             cmp [W_Rel],0           ;Relativity check
  561.             je vl_norel
  562.  
  563.             add ax,[W_Y1]           ;Adjust to window
  564.             add bx,[W_X1]
  565.             add cx,[W_Y1]
  566.  
  567. vl_norel:   cmp ax,cx               ;Y1 > Y2?
  568.             jle vl_yok
  569.             xchg ax,cx              ;Switch them
  570.  
  571. vl_yok:     cmp bx,[W_X2]           ;X > W_X2?
  572.             jg vl_done
  573.             cmp bx,[W_X1]           ;X < W_X1?
  574.             jl vl_done
  575.             cmp ax,[W_Y2]           ;Y1 > W_Y2?
  576.             jg vl_done
  577.             cmp cx,[W_Y1]           ;Y2 < W_Y1?
  578.             jl vl_done
  579.  
  580. vl_cy1:     cmp ax,[W_Y1]           ;Y1 < W_Y1?
  581.             jnl vl_cy2
  582.             mov ax,[W_Y1]           ;Fix Y1
  583.  
  584. vl_cy2:     cmp cx,[W_Y2]           ;Y2 > W_Y2?
  585.             jng vl_cont
  586.             mov cx,[W_Y2]           ;Fix Y2
  587.  
  588. vl_cont:    mov [bp+10],ax          ;Store end-points
  589.             mov [bp+6],bx
  590.             mov [bp+8],cx
  591.  
  592.             mov bx,[bp+6]           ;BX = X
  593.             mov cx,[bp+10]          ;CX = Y
  594.             mov dx,[bp+8]           ;DX = count
  595.             sub dx,cx
  596.             inc dx
  597.             mov al,[bp+4]           ;AL = char
  598.             mov ah,[byte Attr]      ;AH = attr
  599.  
  600.             SET_VROW_CHA            ;Draw row
  601.  
  602. vl_done:    popa                    ;Restore registers
  603.             pop es
  604.             pop bp                  ;Delete stack frame
  605.             ret 8                   ;Return
  606.  
  607. EndP        vline
  608.  
  609. End
  610.  
  611. ~~~C_TXBOX
  612. Ideal
  613.  
  614. Include     "textmac.inc"           ;Include macros
  615. Include     "textdat.inc"           ;Include data
  616.  
  617. Extrn       hline:near, vline:near
  618. Public      box
  619.  
  620. Model Tiny
  621. P186
  622. CodeSeg
  623.  
  624. ;****************** box() -- Draw box, single character
  625. ;void box(int x1, int y1, int x2, int y2, int ch);
  626.  
  627. Proc        box
  628.  
  629.             push bp                 ;Set up stack frame
  630.             mov bp,sp
  631.             pusha                   ;Save registers
  632.  
  633.             mov ax,[bp+12]          ;Get values
  634.             mov bx,[bp+10]
  635.             mov cx,[bp+8]
  636.             mov dx,[bp+6]
  637.  
  638.             cmp [W_Rel],0           ;Relativity check
  639.             je bx_norel
  640.  
  641.             add ax,[W_X1]           ;Adjust to window
  642.             add bx,[W_Y1]
  643.             add cx,[W_X1]
  644.             add dx,[W_Y1]
  645.  
  646. bx_norel:   mov [bp+12],ax          ;Store values
  647.             mov [bp+10],bx
  648.             mov [bp+8],cx
  649.             mov [bp+6],dx
  650.  
  651.             mov si,[bp+4]           ;SI = char
  652.  
  653.             push ax bx cx si        ;Draw top line
  654.             call hline
  655.             push ax cx dx si        ;Draw bottom line
  656.             call hline
  657.             push bx dx ax si        ;Draw left line
  658.             call vline
  659.             push bx dx cx si        ;Draw right line
  660.             call vline
  661.  
  662.             popa                    ;Restore registers
  663.             pop bp                  ;Delete stack frame
  664.             ret 10                  ;Return
  665.  
  666. EndP        box
  667.  
  668. End
  669.  
  670. ~~~C_STYBOX
  671. Ideal
  672.  
  673. Include     "textmac.inc"           ;Include macros
  674. Include     "textdat.inc"           ;Include data
  675.  
  676. Extrn       hline:near, vline:near
  677. Public      hsline, vsline, sbox
  678.  
  679. Model Tiny
  680. P186
  681. CodeSeg
  682.  
  683. STYLEdata   db '      '             ;Box style data: (0 = blank)
  684.             db '─│┌┐└┘'             ;┌─┬─┐ ╔═╦═╗ ╒═╤═╕ ╓─╥─╖
  685.             db '═║╔╗╚╝'             ;├─┼─┤ ╠═╬═╣ ╞═╪═╡ ╟─╫─╢
  686.             db '═│╒╕╘╛'             ;└─┴─┘ ╚═╩═╝ ╘═╧═╛ ╙─╨─╜
  687.             db '─║╓╖╙╜'             ;  1     2     3     4
  688.  
  689. ;****************** sbox() -- Draw box, with style
  690. ;void sbox(int x1, int y1, int x2, int y2, int style);
  691.  
  692. Proc        sbox
  693.  
  694.             push bp                 ;Set up stack frame
  695.             mov bp,sp
  696.             pusha                   ;Save registers
  697.             push es
  698.  
  699.             mov es,[CurSeg]         ;ES = video memory
  700.  
  701.             mov ax,[bp+12]          ;Get values
  702.             mov bx,[bp+10]
  703.             mov cx,[bp+8]
  704.             mov dx,[bp+6]
  705.  
  706.             cmp [W_Rel],0           ;Relativity check
  707.             je sbx_norel
  708.  
  709.             add ax,[W_X1]           ;Adjust to window
  710.             add bx,[W_Y1]
  711.             add cx,[W_X1]
  712.             add dx,[W_Y1]
  713.  
  714. sbx_norel:  mov [bp+12],ax          ;Store values
  715.             mov [bp+10],bx
  716.             mov [bp+8],cx
  717.             mov [bp+6],dx
  718.  
  719.             mov si,[bp+4]           ;SI = style
  720.  
  721.             inc ax                  ;Set up for hsline
  722.             dec cx
  723.  
  724.             push ax cx bx si        ;Draw top line
  725.             call hsline
  726.             push ax cx dx si        ;Draw bottom line
  727.             call hsline
  728.  
  729.             dec ax                  ;Set up for vsline
  730.             inc cx
  731.             inc bx
  732.             dec dx
  733.  
  734.             push bx dx ax si        ;Draw left line
  735.             call vsline
  736.             push bx dx cx si        ;Draw right line
  737.             call vsline
  738.  
  739.             mov bx,[bp+4]           ;AL = TLC char
  740.             imul bx,6
  741.             mov al,[STYLEdata+bx+2]
  742.  
  743.             mov bx,[bp+12]          ;Draw TLC . . .
  744.             mov cx,[bp+10]
  745.  
  746.             cmp cx,[W_Y1]           ;Clip character
  747.             jl sbx_skip2
  748.             cmp cx,[W_Y2]
  749.             jg sbx_skip2
  750.             cmp bx,[W_X1]
  751.             jl sbx_skip1
  752.             cmp bx,[W_X2]
  753.             jg sbx_skip1
  754.  
  755.             mov ah,[byte Attr]      ;AH = attr
  756.             SET_CHA                 ;Draw character
  757.  
  758. sbx_skip1:  mov bx,[bp+4]           ;AL = TRC char
  759.             imul bx,6
  760.             mov al,[STYLEdata+bx+3]
  761.  
  762.             mov bx,[bp+8]           ;Draw TRC . . .
  763.             mov cx,[bp+10]
  764.  
  765.             cmp bx,[W_X1]           ;Clip character
  766.             jl sbx_skip2
  767.             cmp bx,[W_X2]
  768.             jg sbx_skip2
  769.  
  770.             mov ah,[byte Attr]      ;AH = attr
  771.             SET_CHA                 ;Draw character
  772.  
  773. sbx_skip2:  mov bx,[bp+4]           ;AL = LLC char
  774.             imul bx,6
  775.             mov al,[STYLEdata+bx+4]
  776.  
  777.             mov bx,[bp+12]          ;Draw LLC . . .
  778.             mov cx,[bp+6]
  779.  
  780.             cmp cx,[W_Y1]           ;Clip character
  781.             jl sbx_done
  782.             cmp cx,[W_Y2]
  783.             jg sbx_done
  784.             cmp bx,[W_X1]
  785.             jl sbx_skip3
  786.             cmp bx,[W_X2]
  787.             jg sbx_skip3
  788.  
  789.             mov ah,[byte Attr]      ;AH = attr
  790.             SET_CHA                 ;Draw character
  791.  
  792. sbx_skip3:  mov bx,[bp+4]           ;AL = LRC char
  793.             imul bx,6
  794.             mov al,[STYLEdata+bx+5]
  795.  
  796.             mov bx,[bp+8]           ;Draw LRC . . .
  797.             mov cx,[bp+6]
  798.  
  799.             cmp bx,[W_X1]           ;Clip character
  800.             jl sbx_done
  801.             cmp bx,[W_X2]
  802.             jg sbx_done
  803.  
  804.             mov ah,[byte Attr]      ;AH = attr
  805.             SET_CHA                 ;Draw character
  806.  
  807. sbx_done:   pop es                  ;Restore registers
  808.             popa
  809.             pop bp                  ;Delete stack frame
  810.             ret 10                  ;Return
  811.  
  812. EndP        sbox
  813.  
  814. ;****************** hsline() -- Draw horizontal style line
  815. ;void hsline(int x1, int x2, int y, int style);
  816.  
  817. Proc        hsline
  818.  
  819.             push bp                 ;Set up stack frame
  820.             mov bp,sp
  821.             pusha                   ;Save all registers
  822.  
  823.             mov bx,[bp+4]           ;AL = HL char
  824.             imul bx,6
  825.             mov al,[STYLEdata+bx]
  826.             xor ah,ah
  827.  
  828.             push [bp+10] [bp+8]     ;Push args
  829.             push [bp+6] ax
  830.             call hline              ;Draw line
  831.  
  832.             popa                    ;Restore registers
  833.             pop bp                  ;Delete stack frame
  834.             ret 8                   ;Return
  835.  
  836. EndP        hsline
  837.  
  838. ;****************** vsline() -- Draw vertical style line
  839. ;void vsline(int y1, int y2, int x, int style);
  840.  
  841. Proc        vsline
  842.  
  843.             push bp                 ;Set up stack frame
  844.             mov bp,sp
  845.             pusha                   ;Save all registers
  846.  
  847.             mov bx,[bp+4]           ;AL = VL char
  848.             imul bx,6
  849.             mov al,[STYLEdata+bx+1]
  850.             xor ah,ah
  851.  
  852.             push [bp+10] [bp+8]     ;Push args
  853.             push [bp+6] ax
  854.             call vline              ;Draw line
  855.  
  856.             popa                    ;Restore registers
  857.             pop bp                  ;Delete stack frame
  858.             ret 8                   ;Return
  859.  
  860. EndP        vsline
  861.  
  862. End
  863.  
  864. ~~~C_TEXCUR
  865. Ideal
  866.  
  867. Include     "textmac.inc"           ;Include macros
  868. Include     "textdat.inc"           ;Include data
  869.  
  870. Public      gotoxy, getx, gety, setctype, getctype
  871.  
  872. Model Tiny
  873. P186
  874. CodeSeg
  875.  
  876. ;****************** gotoxy() -- Set cursor position
  877. ;void gotoxy(int x, int y);
  878.  
  879. Proc        gotoxy
  880.  
  881.             push bp                 ;Set up stack frame
  882.             mov bp,sp
  883.             pusha                   ;Save all registers
  884.  
  885.             mov ax,[bp+6]           ;Get values
  886.             mov bx,[bp+4]
  887.  
  888.             cmp [W_Rel],0           ;Relativity check
  889.             je xy_norel
  890.  
  891.             add ax,[W_X1]           ;Adjust to window
  892.             add bx,[W_Y1]
  893.  
  894. xy_norel:   cmp ax,[W_X1]           ;Clip to window
  895.             jl xy_done
  896.             cmp ax,[W_X2]
  897.             jg xy_done
  898.             cmp bx,[W_Y1]
  899.             jl xy_done
  900.             cmp bx,[W_Y2]
  901.             jg xy_done
  902.  
  903.             shl bx,7                ;Get CRTC address
  904.             add bx,ax
  905.  
  906.             mov dx,03D4h            ;CRTC port
  907.             mov al,0Eh              ;Cursor Loc High
  908.             mov ah,bh               ;Set value
  909.             out dx,ax
  910.             inc ax                  ;Cursor Loc Low
  911.             mov ah,bl               ;Set value
  912.             out dx,ax
  913.  
  914. xy_done:    popa                    ;Restore registers
  915.             pop bp                  ;Delete stack frame
  916.             ret 4                   ;Return
  917.  
  918. EndP        gotoxy
  919.  
  920. ;****************** getx() -- Get cursor X position
  921. ;int getx(void);
  922.  
  923. Proc        getx
  924.  
  925.             push dx                 ;Save DX
  926.  
  927.             mov dx,03D4h            ;CRTC port
  928.             mov al,0Fh              ;Cursor Loc Low
  929.             out dx,al               ;Set register
  930.             inc dx                  ;Data port
  931.             in al,dx                ;Read value
  932.  
  933.             and ax,127              ;Get X position
  934.  
  935.             cmp [W_Rel],0           ;Relativity check
  936.             je gx_done
  937.             sub ax,[W_X1]           ;Adjust to window
  938.  
  939. gx_done:    pop dx                  ;Restore DX
  940.             ret                     ;Return
  941.  
  942. EndP        getx
  943.  
  944. ;****************** gety() -- Get cursor Y position
  945. ;int gety(void);
  946.  
  947. Proc        gety
  948.  
  949.             push dx                 ;Save DX
  950.  
  951.             mov dx,03D4h            ;CRTC port
  952.             mov al,0Eh              ;Cursor Loc High
  953.             out dx,al               ;Set register
  954.             inc dx                  ;Data port
  955.             in al,dx                ;Read value
  956.             mov ah,al
  957.             dec dx                  ;Index port
  958.             mov al,0Fh              ;Cursor Loc Low
  959.             out dx,al               ;Set register
  960.             inc dx                  ;Data port
  961.             in al,dx                ;Read value
  962.  
  963.             shr ax,7                ;Get Y position
  964.             and ax,127
  965.  
  966.             cmp [W_Rel],0           ;Relativity check
  967.             je gy_done
  968.             sub ax,[W_Y1]           ;Adjust to window
  969.  
  970. gy_done:    pop dx                  ;Restore DX
  971.             ret                     ;Return
  972.  
  973. EndP        gety
  974.  
  975. ;****************** setctype() -- Set cursor type
  976. ;void setctype(int type);
  977.  
  978. Proc        setctype
  979.  
  980.             push bp                 ;Set up stack frame
  981.             mov bp,sp
  982.             pusha                   ;Save all registers
  983.  
  984.             mov bx,[bp+4]           ;Get type
  985.  
  986.             mov dx,03D4h            ;CRTC port
  987.             mov al,0Ah              ;Cursor Start
  988.             mov ah,bh               ;Set value
  989.             out dx,ax
  990.             inc ax                  ;Cursor End
  991.             mov ah,bl               ;Set value
  992.             out dx,ax
  993.  
  994.             popa                    ;Restore registers
  995.             pop bp                  ;Delete stack frame
  996.             ret 2                   ;Return
  997.  
  998. EndP        setctype
  999.  
  1000. ;****************** getctype() -- Get cursor type
  1001. ;int getctype(void);
  1002.  
  1003. Proc        getctype
  1004.  
  1005.             push dx                 ;Save DX
  1006.  
  1007.             mov dx,03D4h            ;CRTC port
  1008.             mov al,0Ah              ;Cursor Start
  1009.             out dx,al               ;Set register
  1010.             inc dx                  ;Data port
  1011.             in al,dx                ;Read value
  1012.             mov ah,al
  1013.             dec dx                  ;Index port
  1014.             mov al,0Bh              ;Cursor End
  1015.             out dx,al               ;Set register
  1016.             inc dx                  ;Data port
  1017.             in al,dx                ;Read value
  1018.  
  1019.             pop dx                  ;Restore DX
  1020.             ret                     ;Return
  1021.  
  1022. EndP        getctype
  1023.  
  1024. End
  1025.  
  1026. ~~~C_TPUTS
  1027. Ideal
  1028.  
  1029. Include     "textmac.inc"           ;Include macros
  1030. Include     "textdat.inc"           ;Include data
  1031.  
  1032. Public      tputs
  1033.  
  1034. Model Tiny
  1035. P186
  1036. CodeSeg
  1037.  
  1038. ;****************** tputs() -- Put string
  1039. ;void tputs(int x, int y, char *str);
  1040.  
  1041. Proc        tputs
  1042.  
  1043.             push bp                 ;Set up stack frame
  1044.             mov bp,sp
  1045.             push es                 ;Save registers
  1046.             pusha
  1047.             mov es,[CurSeg]         ;ES = video memory
  1048.  
  1049.             mov ax,[bp+8]           ;Get values
  1050.             mov bx,[bp+6]
  1051.  
  1052.             cmp [W_Rel],0           ;Relativity check
  1053.             je ps_norel
  1054.  
  1055.             add ax,[W_X1]           ;Adjust to window
  1056.             add bx,[W_Y1]
  1057.             mov [bp+8],ax
  1058.             mov [bp+6],bx
  1059.  
  1060. ps_norel:   cmp bx,[W_Y1]           ;Y < W_Y1?
  1061.             jl ps_done
  1062.             cmp bx,[W_Y2]           ;Y > W_Y2?
  1063.             jg ps_done
  1064.             cmp ax,[W_X2]           ;X > W_X2?
  1065.             jg ps_done
  1066.  
  1067.             shl bx,8                ;DI = offset
  1068.             add bx,ax
  1069.             add bx,ax
  1070.             mov di,bx
  1071.  
  1072.             mov si,[bp+4]           ;SI = string
  1073.             mov bx,[bp+8]           ;BX = X
  1074.             mov cx,[W_X1]           ;CX = W_X1
  1075.             mov dx,[W_X2]           ;DX = W_X2
  1076.             mov ah,[byte Attr]      ;AH = color
  1077.  
  1078. ps_loop:    lodsb                   ;Load char
  1079.             test al,al              ;Null, done
  1080.             je ps_done
  1081.             cmp bx,cx               ;Off left, loop
  1082.             jl ps_lb
  1083.             cmp bx,dx
  1084.             jg ps_done              ;Off right, quit
  1085.  
  1086.             mov [es:di],ax          ;Write char
  1087.  
  1088. ps_lb:      inc di                  ;Advance pointer
  1089.             inc di
  1090.             inc bx                  ;Advance X
  1091.             jmp ps_loop             ;Loop back
  1092.  
  1093. ps_done:    popa                    ;Restore registers
  1094.             pop es bp               ;Delete stack frame
  1095.             ret 6                   ;Return
  1096.  
  1097. EndP        tputs
  1098.  
  1099. End
  1100.  
  1101. ~~~C_TPRINT
  1102. Ideal
  1103.  
  1104. Extrn       sprintf:near,tputs:near,allocmem:near,freemem:near
  1105. Public      tprintf
  1106.  
  1107. Model Tiny
  1108. CodeSeg
  1109. P186
  1110.  
  1111. ;****************** tprintf() -- Print formatted string
  1112. ;void tprintf(int x, int y, char *fmt, void *args);
  1113.  
  1114. x           equ bp+10
  1115. y           equ bp+8
  1116. fmt         equ bp+6
  1117. args        equ bp+4
  1118.  
  1119. Proc        tprintf
  1120.  
  1121.             push bp                 ;Set up stack frame
  1122.             mov bp,sp
  1123.             pusha                   ;Save all registers
  1124.  
  1125.             push 400                ;Allocate a 400 byte buffer
  1126.             call allocmem
  1127.             test ax,ax              ;Out of memory?
  1128.             jz tp_done
  1129.  
  1130.             mov bx,ax               ;BX = buffer
  1131.             push bx [fmt] [args]    ;sprintf() into buffer
  1132.             call sprintf
  1133.  
  1134.             push [x] [y] bx         ;Print string to screen
  1135.             call tputs
  1136.  
  1137.             push bx
  1138.             call freemem            ;Free memory
  1139.  
  1140. tp_done:    popa                    ;Restore registers
  1141.             pop bp                  ;Delete stack frame
  1142.             ret 8                   ;Return
  1143.  
  1144. EndP        tprintf
  1145.  
  1146. End
  1147.  
  1148. ~~~C_TXCHAR
  1149. Ideal
  1150.  
  1151. Include     "textmac.inc"           ;Include macros
  1152. Include     "textdat.inc"           ;Include data
  1153.  
  1154. Public      setch, setat, setcha, readcha
  1155.  
  1156. Model Tiny
  1157. P186
  1158. CodeSeg
  1159.  
  1160. ;****************** setch() -- Write character
  1161. ;void setch(int x, int y, int ch);
  1162.  
  1163. Proc        setch
  1164.  
  1165.             push bp                 ;Set up stack frame
  1166.             mov bp,sp
  1167.             pusha                   ;Save all registers
  1168.             push es
  1169.             mov es,[CurSeg]         ;ES = video memory
  1170.  
  1171.             mov bx,[bp+8]           ;Get values
  1172.             mov cx,[bp+6]
  1173.  
  1174.             cmp [W_Rel],0           ;Relativity check
  1175.             je sch_norel
  1176.  
  1177.             add bx,[W_X1]           ;Adjust to window
  1178.             add cx,[W_Y1]
  1179.  
  1180. sch_norel:  cmp bx,[W_X1]           ;X < W_X1?
  1181.             jl sch_done
  1182.             cmp bx,[W_X2]           ;X > W_X2?
  1183.             jg sch_done
  1184.             cmp cx,[W_Y1]           ;Y < W_Y1?
  1185.             jl sch_done
  1186.             cmp cx,[W_Y2]           ;Y > W_Y2?
  1187.             jg sch_done
  1188.  
  1189.             mov al,[bp+4]           ;AL = char
  1190.             SET_CH                  ;Write char
  1191.  
  1192. sch_done:   pop es                  ;Restore registers
  1193.             popa
  1194.             pop bp                  ;Delete stack frame
  1195.             ret 6                   ;Return
  1196.  
  1197. EndP        setch
  1198.  
  1199. ;****************** setat() -- Write attribute
  1200. ;void setat(int x, int y, int at);
  1201.  
  1202. Proc        setat
  1203.  
  1204.             push bp                 ;Set up stack frame
  1205.             mov bp,sp
  1206.             pusha                   ;Save all registers
  1207.             push es
  1208.             mov es,[CurSeg]         ;ES = video memory
  1209.  
  1210.             mov bx,[bp+8]           ;Get values
  1211.             mov cx,[bp+6]
  1212.  
  1213.             cmp [W_Rel],0           ;Relativity check
  1214.             je sa_norel
  1215.  
  1216.             add bx,[W_X1]           ;Adjust to window
  1217.             add cx,[W_Y1]
  1218.  
  1219. sa_norel:   cmp bx,[W_X1]           ;X < W_X1?
  1220.             jl sa_done
  1221.             cmp bx,[W_X2]           ;X > W_X2?
  1222.             jg sa_done
  1223.             cmp cx,[W_Y1]           ;Y < W_Y1?
  1224.             jl sa_done
  1225.             cmp cx,[W_Y2]           ;Y > W_Y2?
  1226.             jg sa_done
  1227.  
  1228.             mov al,[bp+4]           ;AL = attr.
  1229.             SET_AT                  ;Write attr.
  1230.  
  1231. sa_done:    pop es                  ;Restore registers
  1232.             popa
  1233.             pop bp                  ;Delete stack frame
  1234.             ret 6                   ;Return
  1235.  
  1236. EndP        setat
  1237.  
  1238. ;****************** setcha() -- Write char, attr.
  1239. ;void setcha(int x, int y, int ch, int at);
  1240.  
  1241. Proc        setcha
  1242.  
  1243.             push bp                 ;Set up stack frame
  1244.             mov bp,sp
  1245.             pusha                   ;Save all registers
  1246.             push es
  1247.             mov es,[CurSeg]         ;ES = video memory
  1248.  
  1249.             mov bx,[bp+10]          ;Get values
  1250.             mov cx,[bp+8]
  1251.  
  1252.             cmp [W_Rel],0           ;Relativity check
  1253.             je sb_norel
  1254.  
  1255.             add bx,[W_X1]           ;Adjust to window
  1256.             add cx,[W_Y1]
  1257.  
  1258. sb_norel:   cmp bx,[W_X1]           ;X < W_X1?
  1259.             jl sb_done
  1260.             cmp bx,[W_X2]           ;X > W_X2?
  1261.             jg sb_done
  1262.             cmp cx,[W_Y1]           ;Y < W_Y1?
  1263.             jl sb_done
  1264.             cmp cx,[W_Y2]           ;Y > W_Y2?
  1265.             jg sb_done
  1266.  
  1267.             mov al,[bp+6]           ;AL = char
  1268.             mov ah,[bp+4]           ;AH = attr.
  1269.             SET_CHA                 ;Write char, attr.
  1270.  
  1271. sb_done:    pop es                  ;Restore registers
  1272.             popa
  1273.             pop bp                  ;Delete stack frame
  1274.             ret 8                   ;Return
  1275.  
  1276. EndP        setcha
  1277.  
  1278. ;****************** readcha() -- Read character and attribute
  1279. ;int readcha(int x, int y);
  1280.  
  1281. Proc        readcha
  1282.  
  1283.             push bp                 ;Set up stack frame
  1284.             mov bp,sp
  1285.             push es bx              ;Save registers
  1286.             mov es,[CurSeg]         ;ES = video memory
  1287.  
  1288.             mov ax,[bp+6]           ;Get values
  1289.             mov bx,[bp+4]
  1290.  
  1291.             cmp [W_Rel],0           ;Relativity check
  1292.             je rc_norel
  1293.  
  1294.             add ax,[W_X1]           ;Adjust to window
  1295.             add bx,[W_Y1]
  1296.  
  1297. rc_norel:   shl bx,8                ;Get offset
  1298.             add bx,ax
  1299.             add bx,ax
  1300.             mov ax,[es:bx]          ;Read char/attr
  1301.  
  1302.             pop bx es               ;Restore registers
  1303.             pop bp                  ;Delete stack frame
  1304.             ret 4                   ;Return
  1305.  
  1306. EndP        readcha
  1307.  
  1308. End
  1309.  
  1310. ~~~C_GPTEXT
  1311. Ideal
  1312.  
  1313. Include     "textmac.inc"           ;Include macros
  1314. Include     "textdat.inc"           ;Include data
  1315.  
  1316. Public      gettext, puttext
  1317.  
  1318. Model Tiny
  1319. P186
  1320. CodeSeg
  1321.  
  1322. ;****************** gettext() -- Get text image
  1323. ;void gettext(void *buf, int x1, int y1, int x2, int y2);
  1324.  
  1325. Proc        gettext
  1326.  
  1327.             push bp                 ;Set up stack frame
  1328.             mov bp,sp
  1329.             push ds es              ;Save registers
  1330.             pusha
  1331.  
  1332.             mov ds,[CurSeg]         ;DS = video memory
  1333.  
  1334.             push cs                 ;ES:DI = text buffer
  1335.             pop es
  1336.             mov di,[bp+12]
  1337.  
  1338.             mov ax,[bp+10]          ;Get values
  1339.             mov bx,[bp+8]
  1340.             mov cx,[bp+6]
  1341.             mov dx,[bp+4]
  1342.  
  1343.             cmp [cs:W_Rel],0        ;Relativity check
  1344.             je gt_norel
  1345.  
  1346.             add ax,[cs:W_X1]        ;Adjust to window
  1347.             add bx,[cs:W_Y1]
  1348.             add cx,[cs:W_X1]
  1349.             add dx,[cs:W_Y1]
  1350.  
  1351. gt_norel:   cmp ax,cx               ;Put them in order
  1352.             jle $+3
  1353.             xchg ax,cx
  1354.             cmp bx,dx
  1355.             jle $+4
  1356.             xchg bx,dx
  1357.  
  1358.             mov [bp+10],ax          ;Save values
  1359.             mov [bp+8],bx
  1360.             mov [bp+6],cx
  1361.             mov [bp+4],dx
  1362.  
  1363.             mov si,[bp+8]           ;SI = source offset
  1364.             mov ax,[bp+10]
  1365.             shl si,8
  1366.             add si,ax
  1367.             add si,ax
  1368.  
  1369.             mov dx,[bp+6]           ;DX = X distance
  1370.             sub dx,[bp+10]
  1371.             inc dx
  1372.             mov bx,[bp+4]           ;BX = Y distance
  1373.             sub bx,[bp+8]
  1374.             inc bx
  1375.  
  1376.             mov ax,dx               ;Store size
  1377.             stosw
  1378.             mov ax,bx
  1379.             stosw
  1380.  
  1381.             mov ax,256              ;AX = offset adjust
  1382.             sub ax,dx
  1383.             sub ax,dx
  1384.  
  1385. gt_loop:    mov cx,dx               ;Move line
  1386.             rep movsw
  1387.             add si,ax               ;Advance pointers
  1388.             dec bx                  ;Loop back
  1389.             jnz gt_loop
  1390.  
  1391. gt_done:    popa                    ;Restore registers
  1392.             pop es ds
  1393.             pop bp                  ;Delete stack frame
  1394.             ret 10                  ;Return
  1395.  
  1396. EndP        gettext
  1397.  
  1398. ;****************** puttext() -- Put text image
  1399. ;void puttext(void *buf, int x, int y);
  1400.  
  1401. Proc        puttext
  1402.  
  1403.             push bp                 ;Set up stack frame
  1404.             mov bp,sp
  1405.             push es                 ;Save registers
  1406.             pusha
  1407.  
  1408.             mov es,[CurSeg]         ;ES = video memory
  1409.  
  1410.             mov si,[bp+8]           ;SI = text buffer
  1411.  
  1412.             mov ax,[bp+6]           ;Get values
  1413.             mov bx,[bp+4]
  1414.  
  1415.             cmp [W_Rel],0           ;Relativity check
  1416.             je pt_norel
  1417.  
  1418.             add ax,[W_X1]           ;Adjust to window
  1419.             add bx,[W_Y1]
  1420.  
  1421. pt_norel:   mov [bp+6],ax           ;Save values
  1422.             mov [bp+4],bx
  1423.  
  1424.             lodsw                   ;CX = X distance
  1425.             mov cx,ax
  1426.             lodsw                   ;DX = Y distance
  1427.             mov dx,ax
  1428.  
  1429.             mov ax,[bp+6]           ;Restore AX
  1430.  
  1431.             add cx,ax               ;CX, DX = X2, Y2
  1432.             add dx,bx
  1433.             dec cx
  1434.             dec dx
  1435.  
  1436.             cmp ax,[W_X1]           ;Clip to window
  1437.             jl pt_done
  1438.             cmp bx,[W_Y1]
  1439.             jl pt_done
  1440.             cmp cx,[W_X2]
  1441.             jg pt_done
  1442.             cmp dx,[W_Y2]
  1443.             jg pt_done
  1444.  
  1445.             inc cx                  ;Restore distances
  1446.             sub cx,ax
  1447.             inc dx
  1448.             sub dx,bx
  1449.             mov bx,dx               ;DX = X distance
  1450.             mov dx,cx               ;BX = Y distance
  1451.  
  1452.             mov di,[bp+4]           ;DI = dest. offset
  1453.             mov ax,[bp+6]
  1454.             shl di,8
  1455.             add di,ax
  1456.             add di,ax
  1457.  
  1458.             mov ax,256              ;AX = offset adjust
  1459.             sub ax,dx
  1460.             sub ax,dx
  1461.  
  1462. pt_loop:    mov cx,dx               ;Move line
  1463.             rep movsw
  1464.             add di,ax               ;Advance pointers
  1465.             dec bx                  ;Loop back
  1466.             jnz pt_loop
  1467.  
  1468. pt_done:    popa                    ;Restore registers
  1469.             pop es
  1470.             pop bp                  ;Delete stack frame
  1471.             ret 6                   ;Return
  1472.  
  1473. EndP        puttext
  1474.  
  1475. End
  1476.  
  1477. ~~~C_MTEXT
  1478. Ideal
  1479.  
  1480. Include     "textmac.inc"           ;Include macros
  1481. Include     "textdat.inc"           ;Include data
  1482.  
  1483. Public      movetext
  1484.  
  1485. Model Tiny
  1486. P186
  1487. CodeSeg
  1488.  
  1489. ;****************** movetext() -- Move window of text
  1490. ;void movetext(int x1, int y1, int x2, int y2, int x, int y);
  1491.  
  1492. Proc        movetext
  1493.  
  1494.             push bp                 ;Set up stack frame
  1495.             mov bp,sp
  1496.             pusha                   ;Save registers
  1497.             push ds es
  1498.  
  1499.             mov ax,[CurSeg]         ;DS, ES = video memory
  1500.             mov ds,ax
  1501.             mov es,ax
  1502.  
  1503.             mov ax,[bp+14]          ;Get values
  1504.             mov bx,[bp+12]
  1505.             mov cx,[bp+10]
  1506.             mov dx,[bp+8]
  1507.             mov si,[bp+6]
  1508.             mov di,[bp+4]
  1509.  
  1510.             cmp ax,cx               ;Put them in order
  1511.             jle $+3
  1512.             xchg ax,cx
  1513.             cmp bx,dx
  1514.             jle $+4
  1515.             xchg bx,dx
  1516.  
  1517.             mov [bp+14],ax          ;Save values
  1518.             mov [bp+12],bx
  1519.             mov [bp+10],cx
  1520.             mov [bp+8],dx
  1521.             mov [bp+6],si
  1522.             mov [bp+4],di
  1523.  
  1524.             mov ax,[bp+12]          ;Greater offset, move from end
  1525.             cmp ax,[bp+4]           ;Lesser offset, move from start
  1526.             jl mw_rev               ;Same offset, do nothing
  1527.             jg mw_for
  1528.             mov ax,[bp+14]
  1529.             cmp ax,[bp+6]
  1530.             jl mw_rev
  1531.             jg mw_for
  1532.             jmp mw_done
  1533.  
  1534. mw_for:     mov si,[bp+12]          ;SI = source offset
  1535.             mov ax,[bp+14]
  1536.             shl si,8
  1537.             add si,ax
  1538.             add si,ax
  1539.  
  1540.             mov di,[bp+4]           ;DI = dest. offset
  1541.             mov ax,[bp+6]
  1542.             shl di,8
  1543.             add di,ax
  1544.             add di,ax
  1545.  
  1546.             mov dx,[bp+10]          ;DX = X distance
  1547.             sub dx,[bp+14]
  1548.             inc dx
  1549.             mov bx,[bp+8]           ;BX = Y distance
  1550.             sub bx,[bp+12]
  1551.             inc bx
  1552.  
  1553.             mov ax,256              ;AX = offset adjust
  1554.             sub ax,dx
  1555.             sub ax,dx
  1556.  
  1557. mw_floop:   mov cx,dx               ;Move line
  1558.             rep movsw
  1559.             add si,ax               ;Advance pointers
  1560.             add di,ax
  1561.             dec bx                  ;Loop back
  1562.             jnz mw_floop
  1563.             jmp mw_done
  1564.  
  1565. mw_rev:     mov si,[bp+12]          ;SI = source offset
  1566.             mov ax,[bp+14]
  1567.             shl si,8
  1568.             add si,ax
  1569.             add si,ax
  1570.  
  1571.             mov di,[bp+4]           ;DI = dest. offset
  1572.             mov ax,[bp+6]
  1573.             shl di,8
  1574.             add di,ax
  1575.             add di,ax
  1576.  
  1577.             mov dx,[bp+10]          ;DX = X distance
  1578.             sub dx,[bp+14]
  1579.             mov bx,[bp+8]           ;BX = Y distance
  1580.             sub bx,[bp+12]
  1581.  
  1582.             mov ax,bx               ;Move offsets to end
  1583.             shl ax,8
  1584.             add ax,dx
  1585.             add ax,dx
  1586.             add si,ax
  1587.             add di,ax
  1588.  
  1589.             inc dx                  ;Fix distances
  1590.             inc bx
  1591.  
  1592.             mov ax,256              ;AX = offset adjust
  1593.             sub ax,dx
  1594.             sub ax,dx
  1595.             std                     ;Direction is reverse
  1596.  
  1597. mw_rloop:   mov cx,dx               ;Move line
  1598.             rep movsw
  1599.             sub si,ax               ;Advance pointers
  1600.             sub di,ax
  1601.             dec bx                  ;Loop back
  1602.             jnz mw_rloop
  1603.  
  1604. mw_done:    cld                     ;Clear direction flag
  1605.             pop es ds               ;Restore registers
  1606.             popa
  1607.             pop bp                  ;Delete stack frame
  1608.             ret 12                  ;Return
  1609.  
  1610. EndP        movetext
  1611.  
  1612. End
  1613.  
  1614. ~~~C_SCROLL
  1615. Ideal
  1616.  
  1617. Include     "textmac.inc"           ;Include macros
  1618. Include     "textdat.inc"           ;Include data
  1619.  
  1620. Extrn       movetext:near, hline:near
  1621. Extrn       vline:near, clrwin:near
  1622. Public      scroll
  1623.  
  1624. Model Tiny
  1625. P186
  1626. CodeSeg
  1627.  
  1628. ;****************** scroll() -- Scroll window
  1629. ;void scroll(int dir);
  1630.  
  1631. Proc        scroll
  1632.  
  1633.             push bp                 ;Set up stack frame
  1634.             mov bp,sp
  1635.             pusha                   ;Save registers
  1636.  
  1637.             mov bx,[bp+4]           ;Get direction
  1638.             and bx,3                ;Jump to routine
  1639.             add bx,bx
  1640.             jmp [ScTable+bx]
  1641.  
  1642. sc_up:      mov ax,[W_Y1]           ;Window one line?
  1643.             cmp ax,[W_Y2]
  1644.             jne sc_cont1
  1645.             jmp sc_tiny
  1646.  
  1647. sc_cont1:   inc ax
  1648.             push [W_X1] ax          ;Push args
  1649.             push [W_X2] [W_Y2]
  1650.             push [W_X1] [W_Y1]
  1651.             call movetext           ;Scroll up
  1652.  
  1653.             push [W_X1] [W_X2]      ;Clear bottom line
  1654.             push [W_Y2] ' '
  1655.             call hline
  1656.             jmp sc_done
  1657.  
  1658. sc_down:    mov ax,[W_Y1]           ;Window one line?
  1659.             mov bx,[W_Y2]
  1660.             cmp ax,bx
  1661.             jne sc_cont2
  1662.             jmp sc_tiny
  1663.  
  1664. sc_cont2:   inc ax
  1665.             dec bx
  1666.             push [W_X1] [W_Y1] [W_X2]
  1667.             push bx [W_X1] ax       ;Push args
  1668.             call movetext           ;Scroll down
  1669.  
  1670.             push [W_X1] [W_X2]      ;Clear top line
  1671.             push [W_Y1] ' '
  1672.             call hline
  1673.             jmp sc_done
  1674.  
  1675. sc_left:    mov ax,[W_X1]           ;Window one column?
  1676.             cmp ax,[W_X2]
  1677.             je sc_tiny
  1678.  
  1679.             inc ax
  1680.             push ax [W_Y1]          ;Push args
  1681.             push [W_X2] [W_Y2]
  1682.             push [W_X1] [W_Y1]
  1683.             call movetext           ;Scroll left
  1684.  
  1685.             push [W_Y1] [W_Y2]      ;Clear right column
  1686.             push [W_X2] ' '
  1687.             call vline
  1688.             jmp sc_done
  1689.  
  1690. sc_right:   mov ax,[W_X1]           ;Window one column?
  1691.             mov bx,[W_X2]
  1692.             cmp ax,bx
  1693.             je sc_tiny
  1694.  
  1695.             inc ax
  1696.             dec bx
  1697.             push [W_X1] [W_Y1] bx   ;Push args
  1698.             push [W_Y2] ax [W_Y1]
  1699.             call movetext           ;Scroll right
  1700.  
  1701.             push [W_Y1] [W_Y2]      ;Clear left column
  1702.             push [W_X1] ' '
  1703.             call vline
  1704.             jmp sc_done
  1705.  
  1706. sc_tiny:    call clrwin             ;Clear window
  1707.  
  1708. sc_done:    popa                    ;Restore registers
  1709.             pop bp                  ;Delete stack frame
  1710.             ret 2                   ;Return
  1711.  
  1712. ScTable     dw offset sc_up
  1713.             dw offset sc_down
  1714.             dw offset sc_left
  1715.             dw offset sc_right
  1716.  
  1717. EndP        scroll
  1718.  
  1719. End
  1720.  
  1721. ~~~C_DILINE
  1722. Ideal
  1723.  
  1724. Include     "textmac.inc"           ;Include macros
  1725. Include     "textdat.inc"           ;Include data
  1726.  
  1727. Extrn       movetext:near, hline:near, vline:near
  1728. Public      delline, insline
  1729.  
  1730. Model Tiny
  1731. P186
  1732. CodeSeg
  1733.  
  1734. ;****************** delline() -- Delete line from window
  1735. ;void delline(int y);
  1736.  
  1737. Proc        delline
  1738.  
  1739.             push bp                 ;Set up stack frame
  1740.             mov bp,sp
  1741.             pusha                   ;Save registers
  1742.  
  1743.             mov ax,[bp+4]
  1744.  
  1745.             cmp [W_Rel],0           ;Relativity check
  1746.             je dl_norel
  1747.  
  1748.             add ax,[W_Y1]           ;Adjust to window
  1749.  
  1750. dl_norel:   cmp ax,[W_Y1]           ;Clip to window
  1751.             jl dl_done
  1752.             cmp ax,[W_Y2]
  1753.             jg dl_done
  1754.             je dl_clear
  1755.  
  1756.             inc ax
  1757.             push [W_X1] ax [W_X2]   ;Push args
  1758.             dec ax
  1759.             push [W_Y2] [W_X1] ax
  1760.             call movetext           ;Scroll up
  1761.  
  1762. dl_clear:   push [W_X1] [W_X2]      ;Clear bottom line
  1763.             push [W_Y2] ' '
  1764.             call hline
  1765.  
  1766. dl_done:    popa                    ;Restore registers
  1767.             pop bp                  ;Delete stack frame
  1768.             ret 2                   ;Return
  1769.  
  1770. EndP        delline
  1771.  
  1772. ;****************** insline() -- Insert line in window
  1773. ;void insline(int y);
  1774.  
  1775. Proc        insline
  1776.  
  1777.             push bp                 ;Set up stack frame
  1778.             mov bp,sp
  1779.             pusha                   ;Save registers
  1780.  
  1781.             mov ax,[bp+4]
  1782.  
  1783.             cmp [W_Rel],0           ;Relativity check
  1784.             je il_norel
  1785.  
  1786.             add ax,[W_Y1]           ;Adjust to window
  1787.  
  1788. il_norel:   cmp ax,[W_Y1]           ;Clip to window
  1789.             jl il_done
  1790.             cmp ax,[W_Y2]
  1791.             jg il_done
  1792.             je il_clear
  1793.  
  1794.             mov bx,ax               ;BX = y - 1,
  1795.             mov cx,[W_Y2]           ;CX = W_Y2 - 1
  1796.             inc bx
  1797.             dec cx
  1798.             push [W_X1] ax [W_X2]   ;Push args
  1799.             push cx [W_X1] bx
  1800.             call movetext           ;Scroll down
  1801.  
  1802. il_clear:   push [W_X1] [W_X2]      ;Clear bottom line
  1803.             push ax ' '
  1804.             call hline
  1805.  
  1806. il_done:    popa                    ;Restore registers
  1807.             pop bp                  ;Delete stack frame
  1808.             ret 2                   ;Return
  1809.  
  1810. EndP        insline
  1811.  
  1812. End
  1813.  
  1814. ~~~C_TGETLN
  1815. Ideal
  1816.  
  1817. Include     "textmac.inc"           ;Include macros
  1818. Include     "textdat.inc"           ;Include data
  1819.  
  1820. Extrn       tputs:near,setctype:near,gety:near
  1821. Extrn       gotoxy:near,getx:near,getctype:near
  1822. Extrn       memset:near,hline:near
  1823. Public      getline,setglchar
  1824.  
  1825. Model Tiny
  1826. P186
  1827. CodeSeg
  1828.  
  1829. ;****************** setglchar() -- Set field fill char for getline()
  1830. ;void setglchar(int chr);
  1831.  
  1832. chr         equ bp+4
  1833.  
  1834. Proc        setglchar
  1835.  
  1836.             push bp                 ;Set up stack frame
  1837.             mov bp,sp
  1838.  
  1839.             push [chr]              ;Set gl_fillchar
  1840.             pop [gl_fillchar]
  1841.  
  1842.             pop bp                  ;Delete stack frame
  1843.             ret 2                   ;Return
  1844.  
  1845. EndP        setglchar
  1846.  
  1847. ;****************** getline() -- Get line of text, with edit
  1848. ;void getline(int x, int y, char *strp, int min, int max);
  1849.  
  1850. x           equ bp+12
  1851. y           equ bp+10
  1852. strp        equ bp+8
  1853. min         equ bp+6
  1854. max         equ bp+4
  1855.  
  1856. Proc        getline
  1857.  
  1858.             push bp                 ;Set up stack frame
  1859.             mov bp,sp
  1860.             pusha                   ;Save registers
  1861.  
  1862.             call getctype           ;Save cursor type
  1863.             mov [gl_buf],ax
  1864.             call getx               ;Save cursor position
  1865.             mov [gl_buf+2],ax
  1866.             call gety
  1867.             mov [gl_buf+4],ax
  1868.  
  1869.             mov [gl_iflag],1        ;Insert initially on
  1870.  
  1871.             mov bx,[strp]           ;Clear the string buffer
  1872.             mov si,[max]
  1873.             push bx si 0
  1874.             call memset
  1875.  
  1876.             mov di,si               ;DI = maximum length
  1877.             xor si,si               ;String pos = 0
  1878.             sub di,2
  1879.             mov cx,1                ;Set modify flag
  1880.  
  1881. gl_loop:    call gl_redraw          ;Redraw entry field
  1882.  
  1883. gl_kloop:   xor ax,ax               ;Get a key
  1884.             int 16h
  1885.             cmp al,0Dh              ;Enter?
  1886.             je gl_enter
  1887.             cmp al,1Bh              ;Escape?
  1888.             je gl_esc
  1889.             cmp al,08h              ;Backspace?
  1890.             je gl_bksp
  1891.             test al,al              ;Alphanumeric char?
  1892.             jne gl_char
  1893.             cmp ah,4Bh              ;Left?
  1894.             je gl_left
  1895.             cmp ah,4Dh              ;Right?
  1896.             je gl_right
  1897.             cmp ah,47h              ;Home?
  1898.             je gl_home
  1899.             cmp ah,4Fh              ;End?
  1900.             je gl_end
  1901.             cmp ah,52h              ;Insert?
  1902.             jne gl_skip0
  1903.             jmp gl_insert
  1904. gl_skip0:   cmp ah,53h              ;Delete?
  1905.             jne gl_kloop            ;Invalid, loop 
  1906.             jmp gl_delete
  1907.  
  1908. ;****************** Key Processing Loops
  1909.  
  1910. gl_char:    mov dl,al               ;DL = char
  1911.             mov ax,[gl_iflag]       ;Check insert flag
  1912.             test ax,ax
  1913.             jz gl_skip1
  1914.  
  1915.             push di                 ;Save DI
  1916. gl_sloop1:  mov al,[bx+di-1]        ;Shift string right
  1917.             mov [bx+di],al
  1918.             dec di
  1919.             cmp di,si
  1920.             ja gl_sloop1
  1921.             pop di                  ;Restore DI
  1922.  
  1923. gl_skip1:   inc cx                  ;Set modify flag
  1924.             mov [bx+si],dl          ;Store char
  1925.             cmp si,di               ;Already at right?
  1926.             je gl_loop
  1927.             inc si                  ;Move right
  1928.             jmp gl_loop             ;Loop back
  1929.  
  1930. gl_enter:   push si
  1931.             mov si,di               ;SI = maximum+1
  1932.             inc si
  1933.  
  1934. gl_sloop0:  test si,si              ;Hit the left?
  1935.             jz gl_cont
  1936.             cmp [byte bx+si-1],0    ;End of string?
  1937.             jne gl_cont
  1938.             dec si                  ;Move left
  1939.             jmp gl_sloop0           ;Loop back
  1940. gl_cont:    mov dx,si               ;DX = end point
  1941.             pop si                  ;Restore SI
  1942.  
  1943.             cmp dx,[min]            ;Below minimum, ignore
  1944.             jae gl_finish           ;Otherwise, quit
  1945. gl_jmpk:    jmp gl_kloop
  1946.  
  1947. gl_esc:     push bx di 0            ;Clear buffer...
  1948.             call memset
  1949.             xor si,si
  1950.             call gl_redraw
  1951.             jmp gl_done
  1952.  
  1953. gl_bksp:    test si,si              ;Can't backspace from
  1954.             jz gl_jmpk              ;the first char
  1955.             dec si                  ;Delete at SI-1
  1956.             jmp gl_delete
  1957.  
  1958. gl_left:    test si,si              ;Already at left?
  1959.             jz gl_jmpk
  1960.             dec si                  ;Move left
  1961.             jmp gl_loop             ;Loop back
  1962.  
  1963. gl_right:   cmp si,di               ;Already at right?
  1964.             jae gl_jmpk
  1965.             inc si                  ;Move right
  1966.             jmp gl_loop             ;Loop back
  1967.  
  1968. gl_home:    xor si,si               ;Move to left
  1969. gl_jmp:     jmp gl_loop             ;Loop back
  1970.  
  1971. gl_end:     mov si,di               ;SI = maximum
  1972.             cmp [byte bx+si],0      ;Buffer full, move to end
  1973.             jne gl_jmp
  1974.  
  1975. gl_sloop2:  test si,si              ;Hit the left?
  1976.             jz gl_jmp
  1977.             cmp [byte bx+si-1],0    ;End of string?
  1978.             jne gl_jmp
  1979.             dec si                  ;Move left
  1980.             jmp gl_sloop2           ;Loop back
  1981.  
  1982. gl_insert:  mov ax,1                ;Toggle insert flag
  1983.             sub ax,[gl_iflag]
  1984.             mov [gl_iflag],ax
  1985.             jmp gl_loop             ;Loop back
  1986.             
  1987. gl_delete:  push si                 ;Save SI
  1988.             inc si
  1989. gl_sloop3:  mov ax,[bx+si]          ;Shift string left
  1990.             mov [bx+si-1],al
  1991.             inc si
  1992.             cmp si,di
  1993.             jbe gl_sloop3
  1994.             pop si                  ;Restore SI
  1995.             mov [byte bx+di],0      ;End with space
  1996.             inc cx                  ;Set modify flag
  1997.             jmp gl_loop             ;Loop back
  1998.  
  1999. ;****************** End of Key Processing
  2000.  
  2001. gl_finish:  mov si,dx               ;SI = length
  2002.             mov [byte bx+si],0      ;Terminate with a null
  2003.  
  2004. gl_done:    push [gl_buf+2]         ;Restore cursor position
  2005.             push [gl_buf+4]
  2006.             call gotoxy
  2007.             push [gl_buf]           ;Restore cursor type
  2008.             call setctype
  2009.  
  2010.             popa                    ;Restore registers
  2011.             pop bp                  ;Delete stack frame
  2012.             ret 10                  ;Return
  2013.  
  2014. gl_redraw:  pusha                   ;Save all registers
  2015.             test cx,cx              ;Check modify flag
  2016.             jz gl_setcur
  2017.             mov cx,[x]              ;Set up to clear the field
  2018.             push cx
  2019.             add cx,di               ;Clear the field
  2020.             push cx [y] [gl_fillchar]
  2021.             call hline
  2022.             push [x] [y] bx         ;Show the string
  2023.             call tputs
  2024.  
  2025. gl_setcur:  mov ax,[x]              ;Set the cursor position
  2026.             add ax,si
  2027.             push ax [y]
  2028.             call gotoxy
  2029.  
  2030.             mov bx,[gl_iflag]       ;AX = cursor to use
  2031.             add bx,bx
  2032.             mov ax,[gl_curs+bx]
  2033.             push ax                 ;Set cursor type
  2034.             call setctype
  2035.  
  2036.             popa                    ;Restore registers
  2037.             xor cx,cx               ;Reset modify flag
  2038.             ret                     ;Return
  2039.  
  2040. gl_buf      dw 3 dup(0)             ;Scratch buffer
  2041. gl_iflag    dw 0                    ;Insert flag
  2042.  
  2043. gl_curs     dw 000Dh,0C0Dh          ;Cursors
  2044. gl_fillchar dw 0020h                ;Fill char (space)
  2045.  
  2046. EndP        getline
  2047.  
  2048. End
  2049.  
  2050. ~~~C_AMOUSE
  2051. Ideal
  2052.  
  2053. Public      minit, mclose, mshow, mhide
  2054. Public      mget, mgetdn, mgetup
  2055.  
  2056. Model Tiny
  2057. P186
  2058. CodeSeg
  2059.  
  2060. ;****************** Mouse Data
  2061.  
  2062. M_X         dw 0                    ;Mouse X
  2063. M_Y         dw 0                    ;Mouse Y
  2064.  
  2065. ScrnBuf     dw 0                    ;Screen buffer
  2066.  
  2067. MouseFound  dw 0                    ;Existence flag
  2068. MouseOn     dw 0                    ;Display flag
  2069.  
  2070. ;****************** INTERNAL PROC MouseHandler
  2071.  
  2072. Proc        MouseHandler
  2073.  
  2074.             cmp [cs:MouseOn],0      ;Cursor off, skip
  2075.             je MH_Skip
  2076.             call PutCursor          ;Toggle cursor off
  2077.             mov [cs:M_X],cx         ;Set X, Y
  2078.             mov [cs:M_Y],dx
  2079.             call PutCursor          ;Toggle cursor on
  2080. MH_Skip:    retf                    ;Return
  2081.  
  2082. EndP        MouseHandler
  2083.  
  2084. ;****************** INTERNAL PROC PutCursor
  2085.  
  2086. Proc        PutCursor
  2087.  
  2088.             pusha                   ;Save all registers
  2089.             push ds
  2090.  
  2091.             push 0A000h             ;DS = video memory
  2092.             pop ds
  2093.  
  2094.             mov bx,[cs:M_Y]         ;AX, BX = X, Y
  2095.             mov ax,[cs:M_X]
  2096.             shl bx,5                ;BX = offset
  2097.             shr ax,2
  2098.             add bx,ax
  2099.  
  2100.             xor [byte bx+1],77h     ;Toggle cursor
  2101.  
  2102.             pop ds                  ;Restore registers
  2103.             popa
  2104.             ret                     ;Return
  2105.  
  2106. EndP        PutCursor
  2107.  
  2108. ;****************** minit() -- Initialize mouse system
  2109. ;int minit(void);
  2110.  
  2111. Proc        minit
  2112.  
  2113.             pusha                   ;Save all registers
  2114.  
  2115.             xor ax,ax               ;Initialize mouse
  2116.             int 33h
  2117.             test ax,ax
  2118.             jz im_nomouse
  2119.  
  2120.             mov ax,7                ;Set X limits
  2121.             xor cx,cx               ;(90 * 8) - 1
  2122.             mov dx,719
  2123.             int 33h
  2124.  
  2125.             mov ax,8                ;Set Y limits
  2126.             xor cx,cx               ;(34 * 8) - 1
  2127.             mov dx,271
  2128.             int 33h
  2129.  
  2130.             mov ax,3                ;Get position
  2131.             int 33h
  2132.  
  2133.             mov [M_X],cx            ;Save position
  2134.             mov [M_Y],dx
  2135.  
  2136.             push es                 ;Save ES
  2137.  
  2138.             push cs                 ;ES:DX = mouse handler
  2139.             pop es
  2140.             mov dx,offset MouseHandler
  2141.             mov ax,0Ch              ;Set handler
  2142.             mov cx,1                ;for motion
  2143.             int 33h
  2144.  
  2145.             mov [MouseOn],0         ;Mouse is off
  2146.  
  2147.             pop es                  ;Restore ES
  2148.  
  2149.             mov [MouseFound],1      ;Set mouse flag
  2150.             popa                    ;Restore registers
  2151.             mov ax,1                ;Return 1
  2152.             ret
  2153.  
  2154. im_nomouse: mov [MouseFound],0      ;Reset mouse flag
  2155.             popa                    ;Restore registers
  2156.             xor ax,ax               ;Return 0
  2157.             ret
  2158.  
  2159. EndP        minit
  2160.  
  2161. ;****************** mclose() -- End mouse system
  2162. ;void mclose(void);
  2163.  
  2164. Proc        mclose
  2165.  
  2166.             mov [MouseFound],0      ;Reset mouse flag
  2167.  
  2168.             cmp [MouseOn],0         ;Mouse off, skip
  2169.             je em_Off
  2170.             mov [MouseOn],0         ;Reset cursor flag
  2171.             call PutCursor          ;Hide mouse cursor
  2172.  
  2173. em_Off:     push ax                 ;Save AX
  2174.             xor ax,ax               ;Reinitialize mouse
  2175.             int 33h
  2176.             pop ax                  ;Restore AX
  2177.             ret                     ;Return
  2178.  
  2179. EndP        mclose
  2180.  
  2181. ;****************** mshow() -- Show mouse cursor
  2182. ;void mshow(void);
  2183.  
  2184. Proc        mshow
  2185.  
  2186.             cmp [MouseOn],1         ;Already on, do nothing
  2187.             je sm_done
  2188.             call PutCursor          ;Show mouse cursor
  2189.             mov [MouseOn],1         ;Set cursor flag
  2190. sm_done:    ret                     ;Return
  2191.  
  2192. EndP        mshow
  2193.  
  2194. ;****************** mhide() -- Hide mouse cursor
  2195. ;void mhide(void);
  2196.  
  2197. Proc        mhide
  2198.  
  2199.             cmp [MouseOn],0         ;Already off, do nothing
  2200.             je hm_done
  2201.             mov [MouseOn],0         ;Reset cursor flag
  2202.             call PutCursor          ;Hide mouse cursor
  2203. hm_done:    ret                     ;Return
  2204.  
  2205. EndP        mhide
  2206.  
  2207. ;****************** mget() -- Get mouse position - returns AX,BX,CX
  2208. ;(int, int, int) mget(void);
  2209.  
  2210. Proc        mget
  2211.  
  2212.             push dx                 ;Save registers
  2213.  
  2214.             mov ax,3                ;Get mouse position
  2215.             int 33h
  2216.  
  2217.             shr cx,3                ;Fix values
  2218.             shr dx,3
  2219.  
  2220.             mov ax,bx               ;Return AX = button state,
  2221.             mov bx,cx               ;       BX = X value, and
  2222.             mov cx,dx               ;       CX = Y value
  2223.  
  2224.             pop dx                  ;Restore registers
  2225.             ret                     ;Return
  2226.  
  2227. EndP        mget
  2228.  
  2229. ;****************** mgetdn() -- Get mouse press info - returns AX,BX,CX
  2230. ;(int, int, int) mgetdn(int btn);
  2231.  
  2232. Proc        mgetdn
  2233.  
  2234.             push bp                 ;Set up stack frame
  2235.             mov bp,sp
  2236.             push dx                 ;Save registers
  2237.  
  2238.             mov ax,5                ;Get mouse press info
  2239.             mov bx,[bp+4]
  2240.             int 33h
  2241.  
  2242.             shr cx,3                ;Fix values
  2243.             shr dx,3
  2244.  
  2245.             mov ax,bx               ;Return AX = press count,
  2246.             mov bx,cx               ;       BX = X value, and
  2247.             mov cx,dx               ;       CX = Y value
  2248.  
  2249.             pop dx                  ;Restore registers
  2250.             pop bp                  ;Delete stack frame
  2251.             ret 2                   ;Return
  2252.  
  2253. EndP        mgetdn
  2254.  
  2255. ;****************** mgetup() -- Get mouse release info - returns AX,BX,CX
  2256. ;(int, int, int) mgetup(int btn);
  2257.  
  2258. Proc        mgetup
  2259.  
  2260.             push bp                 ;Set up stack frame
  2261.             mov bp,sp
  2262.             push dx                 ;Save registers
  2263.  
  2264.             mov ax,6                ;Get mouse release info
  2265.             mov bx,[bp+4]
  2266.             int 33h
  2267.  
  2268.             shr cx,3                ;Fix values
  2269.             shr dx,3
  2270.  
  2271.             mov ax,bx               ;Return AX = release count,
  2272.             mov bx,cx               ;       BX = X value, and
  2273.             mov cx,dx               ;       CX = Y value
  2274.  
  2275.             pop dx                  ;Restore registers
  2276.             pop bp                  ;Delete stack frame
  2277.             ret 2                   ;Return
  2278.  
  2279. EndP        mgetup
  2280.  
  2281. End
  2282.